package com.huaxi.ddgw.user.controller;

import com.github.pagehelper.PageInfo;
import com.huaxi.ddgw.model.dto.h5.UserLoginDto;
import com.huaxi.ddgw.model.dto.h5.UserRegisterDto;
import com.huaxi.ddgw.model.entity.user.UserBrowseHistory;
import com.huaxi.ddgw.model.entity.user.UserCollect;
import com.huaxi.ddgw.model.vo.common.Result;
import com.huaxi.ddgw.model.vo.common.ResultCodeEnum;
import com.huaxi.ddgw.model.vo.h5.UserInfoVo;
import com.huaxi.ddgw.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "会员用户接口")
@RestController
@RequestMapping("api/user/userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("auth/getCurrentUserInfo")
    public Result<UserInfoVo> getCurrentUserInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        UserInfoVo userInfoVo = userInfoService.getCurrentUserInfo(token) ;
        return Result.build(userInfoVo , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "会员登录")
    @PostMapping("login")
    public Result login(@RequestBody UserLoginDto userLoginDto) {
        String token = userInfoService.login(userLoginDto);
        return Result.build(token, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "会员注册")
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto) {
        userInfoService.register(userRegisterDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "新增商品浏览信息")
    @GetMapping("isCollect/{skuId}")
    public Result saveUserCollect(@PathVariable Long skuId) {
        //保存商品浏览信息
        userInfoService.saveUserCollect(skuId);
        return Result.build(null , ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "商品浏览信息分页展示")
    @GetMapping("auth/findUserBrowseHistoryPage/{page}/{limit}")
    public Result findUserBrowseHistoryPage(@PathVariable Integer page, @PathVariable Integer limit) {
        List<UserBrowseHistory> userBrowseHistoryList = userInfoService.findUserBrowseHistoryPage(page, limit);
        PageInfo<UserBrowseHistory> pageInfo = new PageInfo<>(userBrowseHistoryList);
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "添加收藏")
    @GetMapping("/auth/collect/{skuId}")
    public Result saveCollect(@PathVariable Long skuId) {
        userInfoService.saveCollect(skuId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "收藏商品分页展示")
    @GetMapping("auth/findUserCollectPage/{page}/{limit}")
    public Result<UserInfoVo> findUserCollectPage(@PathVariable Integer page, @PathVariable Integer limit) {
        List<UserCollect> userCollectList = userInfoService.findUserCollectPage(page, limit);
        PageInfo<UserCollect> userCollectPageInfo = new PageInfo<>(userCollectList);
        return Result.build(userCollectPageInfo , ResultCodeEnum.SUCCESS) ;
    }
}
